home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic 4 Database How-To
/
Visual Basic 4 Database - How-to (The Waite Group)(1995).iso
/
lockexpl.fr_
/
lockexpl.fr
Wrap
Text File
|
1995-07-06
|
13KB
|
455 lines
VERSION 4.00
Begin VB.Form Form1
BackColor = &H00C0C0C0&
Caption = "Lock Explorer"
ClientHeight = 3960
ClientLeft = 1590
ClientTop = 1650
ClientWidth = 3450
Height = 4365
Left = 1530
LinkTopic = "Form1"
ScaleHeight = 3960
ScaleWidth = 3450
Top = 1305
Width = 3570
Begin VB.CommandButton cmdRefresh
Caption = "&Refresh"
Default = -1 'True
BeginProperty Font
name = "MS Sans Serif"
charset = 0
weight = 700
size = 8.25
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 495
Left = 300
TabIndex = 11
Top = 3180
Width = 1215
End
Begin VB.CommandButton cmdLockPessimistic
Caption = "&Pessimistic Locking"
BeginProperty Font
name = "System"
charset = 0
weight = 700
size = 9.75
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 375
Left = 360
TabIndex = 10
Top = 2640
Width = 2655
End
Begin VB.CommandButton cmdLockOptimistic
Caption = "&Optimistic Locking"
BeginProperty Font
name = "System"
charset = 0
weight = 700
size = 9.75
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 375
Left = 360
TabIndex = 9
Top = 2160
Width = 2655
End
Begin VB.CommandButton cmdClose
Cancel = -1 'True
Caption = "&Close"
BeginProperty Font
name = "System"
charset = 0
weight = 700
size = 9.75
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 495
Left = 1860
TabIndex = 8
Top = 3180
Width = 1215
End
Begin VB.CommandButton cmdNext
Caption = ">"
BeginProperty Font
name = "MS Sans Serif"
charset = 0
weight = 700
size = 13.5
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 435
Left = 2640
TabIndex = 7
Top = 1620
Width = 435
End
Begin VB.TextBox txtPhone
Height = 285
Left = 360
TabIndex = 4
Top = 1140
Width = 1995
End
Begin VB.TextBox txtName
Height = 285
Left = 360
TabIndex = 3
Top = 420
Width = 2655
End
Begin VB.CommandButton cmdUpdate
Caption = "&Update"
BeginProperty Font
name = "System"
charset = 0
weight = 700
size = 9.75
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 435
Left = 1680
TabIndex = 2
Top = 1620
Width = 975
End
Begin VB.CommandButton cmdEdit
Caption = "&Edit"
BeginProperty Font
name = "System"
charset = 0
weight = 700
size = 9.75
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 435
Left = 720
TabIndex = 1
Top = 1620
Width = 975
End
Begin VB.CommandButton cmdPrevious
Caption = "<"
BeginProperty Font
name = "MS Sans Serif"
charset = 0
weight = 700
size = 13.5
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 435
Left = 300
TabIndex = 0
Top = 1620
Width = 435
End
Begin VB.Label Label3
AutoSize = -1 'True
BackColor = &H00C0C0C0&
Caption = "Telephone Number:"
BeginProperty Font
name = "MS Sans Serif"
charset = 0
weight = 700
size = 8.25
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 195
Left = 360
TabIndex = 6
Top = 900
Width = 1680
End
Begin VB.Label Label1
AutoSize = -1 'True
BackColor = &H00C0C0C0&
Caption = "Publisher's Name:"
BeginProperty Font
name = "MS Sans Serif"
charset = 0
weight = 700
size = 8.25
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
Height = 195
Left = 360
TabIndex = 5
Top = 180
Width = 1530
End
End
Attribute VB_Name = "Form1"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Option Explicit
' Declare the recordset at the form level; several routines need to access
' the variable to work with the recordset.
Dim rs As Recordset
Private Sub cmdClose_Click()
End
End Sub
Private Sub cmdEdit_Click()
' Set up error handling.
On Error GoTo EditError
' Fill the copy buffer with the contents of the current record.
' In real code, you would not execute an error and leave it "hanging"
' like this when you exit from the procedure. This is for demnonstration
' purposes only.
rs.Edit
Exit Sub
EditError:
Dim msg As String
' If a predictable locking error message is received, display an error
' message explaining what's going on. Otherwise, just pass through
' Visual Basic's error message.
Select Case Err
Case 3167
msg = "Some dirty rat has deleted the record you are tring to modify!"
Case 3260
msg = "Locking error " & Str$(Err) & " on Edit."
msg = msg & " Pessimistic locking must be enabled!"
Case Else
msg = Error(Err)
End Select
MsgBox msg, vbExclamation
Exit Sub
End Sub
Private Sub cmdLockOptimistic_Click()
' Set optimistic locking by calling the SetLockEdit procedure.
SetLockEdit False
End Sub
Private Sub cmdLockPessimistic_Click()
' Set pessimistic locking by calling the SetLockEdit procedure.
SetLockEdit True
End Sub
Private Sub cmdPrevious_Click()
' Position the record pointer on the previous record in the dynaset.
rs.MovePrevious
If rs.BOF Then
' If that positioned the pointer to BOF (before the first record),
' alert the user with a beep and reposition back to the first record.
rs.MoveNext
Beep
Else
' Display the new record's values in the text boxes.
DisplayRecord
End If
End Sub
Private Sub cmdNext_Click()
' Position the record pointer to the previous record in the dynaset.
rs.MoveNext
If rs.EOF Then
' If that positioned the pointer to EOF (afterthe last record),
' alert the user with a beep and reposition back to the last record.
rs.MovePrevious
Beep
Else
' Display the new record's values in the text boxes.
DisplayRecord
End If
End Sub
Private Sub cmdRefresh_Click()
' Get the current values for the current page of the dynaset from the
' database and display them.
rs.Requery
DisplayRecord
End Sub
Private Sub cmdUpdate_Click()
' Set up error handling.
On Error GoTo UpdateError
' Place the current values of the text boxes into the copy buffer.
rs!Name = txtName
rs!Telephone = txtPhone
' Write the contents of the copy buffer to the current record.
rs.UPDATE
Exit Sub
UpdateError:
Dim msg As String
Select Case Err
Case 3197
' Another user has updated this record since the last time the
' Dynaset was updated. Display a meaningful error message and give
' the user the chance to overwrite the other user's change.
msg = "The data in this record have already been modified by"
msg = msg & " another user. Do you want to overwrite those chenges"
msg = msg & " with your own?"
If MsgBox(msg, vbQuestion + vbYesNo + vbDefaultButton2) = vbYes Then
' The user said yes, so re-execute the Update method. This time
' it should "take."
Resume
Else
' The user said no, so refresh the dynaset with the current
' data and display that data. Then display a message explaining
' what's happened.
rs.Requery
DisplayRecord
msg = "The current values of the record are now displayed."
MsgBox msg, vbInformation
' Exit from the procedure now to bypass the code after the
' End Select statement.
Exit Sub
End If
Case 3020
' The user clicked Update without previously having clicked
' Edit. The default error message is "Update without AddNew
' or Edit." Create an error that is more meaningful in the current
' context. (The message gets displayed after the End Select
' statement).
msg = "You must click Edit before you click Update!"
Case 3260
' Another user has the page locked. Create a meaningful message.
' (The message gets displayed after the End Select statement.)
msg = "Locking error " & Str$(Err) & " on Update."
msg = msg & " Optimistic locking must be enabled!"
Case Else
' An unanticipated error, so just pass through Visual Basic's
' message.
msg = Error(Err)
End Select
MsgBox msg, vbExclamation
Exit Sub
End Sub
Private Sub Form_Load()
Dim db As DATABASE
Dim dbName As String
Dim sql As String
' Get the database name and open the database.
dbName = BiblioPath() ' BiblioPath is a function in READINI.BAS
Set db = DBEngine.Workspaces(0).OpenDatabase(dbName)
' Create a dynaset-type recordset assigned to the module-level Recordset
' variable rs. By default, LockEdits is True when you create a new dynaset
' and therefore, pessimistic locking is enabled.
sql = "SELECT [Name], [Telephone] from [Publishers] ORDER BY [Name]"
Set rs = db.OpenRecordset(sql, dbOpenDynaset)
' Display the first record in the dynaset
DisplayRecord
End Sub
Sub SetLockEdit(lockOnEdit As Boolean)
' Set the LockEdits property as specified by the argument.
rs.LockEdits = lockOnEdit
End Sub
'*******************************************************************************
Private Sub DisplayRecord()
' Fill the text boxes on the form, converting any Nulls in the database
' to zero-length strings.
If Not IsNull(rs!Name) Then txtName = rs!Name Else txtName = ""
If Not IsNull(rs!Telephone) Then txtPhone = rs!Telephone Else txtPhone = ""
End Sub